<html><head><title>Record.js</title><link rel="stylesheet" type="text/css" href="../resources/style.css" media="screen"/></head><body><h1>Record.js</h1><pre class="highlighted"><code><i>/**
* @class Ext.data.Record
 * Instances of <b>this</b> class encapsulate both record &lt;em&gt;definition&lt;/em&gt; information, and record
 * &lt;em&gt;value&lt;/em&gt; information <b>for</b> use <b>in</b> {@link Ext.data.Store} objects, or any code which needs
 * to access Records cached <b>in</b> an {@link Ext.data.Store} object.&lt;br&gt;
 * &lt;p&gt;
 * Constructors <b>for</b> this class are generated by passing an Array of field definition objects to {@link #create}.
 * Instances are usually only created by {@link Ext.data.Reader} implementations when processing unformatted data
 * objects.&lt;br&gt;
 * &lt;p&gt;
 * Record objects generated by <b>this</b> constructor inherit all the methods of Ext.data.Record listed below.
 * @constructor
 * This constructor should not be used to create Record objects. Instead, use the constructor generated by
 * {@link #create}. The parameters are the same.
 * @param {Array} data An associative Array of data values keyed by the field name.
 * @param {Object} id (Optional) The id of the record. This id should be unique, and is used by the
 * {@link Ext.data.Store} object which owns the Record to index its collection of Records. If
 * not specified an integer id is generated.
 */</i>
Ext.data.Record = <b>function</b>(data, id){
    <b>this</b>.id = (id || id === 0) ? id : ++Ext.data.Record.AUTO_ID;
    <b>this</b>.data = data;
};

<i>/**
 * Generate a constructor <b>for</b> a specific record layout.
 * @param {Array} o An Array of field definition objects which specify field names, and optionally,
 * data types, and a mapping <b>for</b> an {@link Ext.data.Reader} to extract the field's value from a data object.
 * Each field definition object may contain the following properties: &lt;ul&gt;
 * &lt;li&gt;&lt;b&gt;name&lt;/b&gt; : String&lt;p style=&quot;margin-left:1em&quot;&gt;The name by which the field is referenced within the Record. This is referenced by,
 * <b>for</b> example the &lt;em&gt;dataIndex&lt;/em&gt; property <b>in</b> column definition objects passed to {@link Ext.grid.ColumnModel}&lt;/p&gt;&lt;/li&gt;
 * &lt;li&gt;&lt;b&gt;mapping&lt;/b&gt; : String&lt;p style=&quot;margin-left:1em&quot;&gt;(Optional) A path specification <b>for</b> use by the {@link Ext.data.Reader} implementation
 * that is creating the Record to access the data value from the data object. If an {@link Ext.data.JsonReader}
 * is being used, then <b>this</b> is a string containing the javascript expression to reference the data relative to 
 * the record item's root. If an {@link Ext.data.XmlReader} is being used, <b>this</b> is an {@link Ext.DomQuery} path
 * to the data item relative to the record element. If the mapping expression is the same as the field name,
 * <b>this</b> may be omitted.&lt;/p&gt;&lt;/li&gt;
 * &lt;li&gt;&lt;b&gt;type&lt;/b&gt; : String&lt;p style=&quot;margin-left:1em&quot;&gt;(Optional) The data type <b>for</b> conversion to displayable value. Possible values are
 * &lt;ul&gt;&lt;li&gt;auto (Default, implies no conversion)&lt;/li&gt;
 * &lt;li&gt;string&lt;/li&gt;
 * &lt;li&gt;int&lt;/li&gt;
 * &lt;li&gt;float&lt;/li&gt;
 * &lt;li&gt;boolean&lt;/li&gt;
 * &lt;li&gt;date&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;/li&gt;
 * &lt;li&gt;&lt;b&gt;sortType&lt;/b&gt; : Mixed&lt;p style=&quot;margin-left:1em&quot;&gt;(Optional) A member of {@link Ext.data.SortTypes}.&lt;/p&gt;&lt;/li&gt;
 * &lt;li&gt;&lt;b&gt;sortDir&lt;/b&gt; : String&lt;p style=&quot;margin-left:1em&quot;&gt;(Optional) Initial direction to sort. &quot;ASC&quot; or &quot;DESC&quot;&lt;/p&gt;&lt;/li&gt;
 * &lt;li&gt;&lt;b&gt;convert&lt;/b&gt; : Function&lt;p style=&quot;margin-left:1em&quot;&gt;(Optional) A <b>function</b> which converts the value provided
 * by the Reader into an object that will be stored <b>in</b> the Record. It is passed the
 * following parameters:&lt;ul&gt;
 * &lt;li&gt;&lt;b&gt;v&lt;/b&gt; : Mixed&lt;p style=&quot;margin-left:1em&quot;&gt;The data value as read by the Reader.&lt;/p&gt;&lt;/li&gt;
 * &lt;/ul&gt;&lt;/p&gt;&lt;/li&gt;
 * &lt;li&gt;&lt;b&gt;dateFormat&lt;/b&gt; : String&lt;p style=&quot;margin-left:1em&quot;&gt;(Optional) A format String <b>for</b> the Date.parseDate <b>function</b>.&lt;/p&gt;&lt;/li&gt;
 * &lt;/ul&gt;
 * &lt;br&gt;usage:&lt;br&gt;&lt;pre&gt;&lt;code&gt;
<b>var</b> TopicRecord = Ext.data.Record.create(
    {name: <em>'title'</em>, mapping: <em>'topic_title'</em>},
    {name: <em>'author'</em>, mapping: <em>'username'</em>},
    {name: <em>'totalPosts'</em>, mapping: <em>'topic_replies'</em>, type: <em>'int'</em>},
    {name: <em>'lastPost'</em>, mapping: <em>'post_time'</em>, type: <em>'date'</em>},
    {name: <em>'lastPoster'</em>, mapping: <em>'user2'</em>},
    {name: <em>'excerpt'</em>, mapping: <em>'post_text'</em>}
);

<b>var</b> myNewRecord = <b>new</b> TopicRecord({
    title: <em>'Do my job please'</em>,
    author: <em>'noobie'</em>,
    totalPosts: 1,
    lastPost: <b>new</b> Date(),
    lastPoster: <em>'Animal'</em>,
    excerpt: <em>'No way dude!'</em>
});
myStore.add(myNewRecord);
&lt;/code&gt;&lt;/pre&gt;
 * @method create
 * @static
 */</i>
Ext.data.Record.create = <b>function</b>(o){
    <b>var</b> f = <b>function</b>(){
        f.superclass.constructor.apply(<b>this</b>, arguments);
    };
    Ext.extend(f, Ext.data.Record);
    <b>var</b> p = f.prototype;
    p.fields = <b>new</b> Ext.util.MixedCollection(false, <b>function</b>(field){
        <b>return</b> field.name;
    });
    <b>for</b>(var i = 0, len = o.length; i &lt; len; i++){
        p.fields.add(<b>new</b> Ext.data.Field(o[i]));
    }
    f.getField = <b>function</b>(name){
        <b>return</b> p.fields.get(name);  
    };
    <b>return</b> f;
};

Ext.data.Record.AUTO_ID = 1000;
Ext.data.Record.EDIT = <em>'edit'</em>;
Ext.data.Record.REJECT = <em>'reject'</em>;
Ext.data.Record.COMMIT = <em>'commit'</em>;

Ext.data.Record.prototype = {
    <i>/**
     * Readonly flag - true <b>if</b> this record has been modified.
     * @type Boolean
     */</i>
    dirty : false,
    editing : false,
    error: null,
    modified: null,

    <i>// private</i>
    join : <b>function</b>(store){
        <b>this</b>.store = store;
    },

    <i>/**
     * Set the named field to the specified value.
     * @param {String} name The name of the field to set.
     * @param {Object} value The value to set the field to.
     */</i>
    set : <b>function</b>(name, value){
        <b>if</b>(this.data[name] == value){
            <b>return</b>;
        }
        <b>this</b>.dirty = true;
        <b>if</b>(!<b>this</b>.modified){
            <b>this</b>.modified = {};
        }
        <b>if</b>(typeof <b>this</b>.modified[name] == <em>'undefined'</em>){
            <b>this</b>.modified[name] = <b>this</b>.data[name];
        }
        <b>this</b>.data[name] = value;
        <b>if</b>(!<b>this</b>.editing){
            <b>this</b>.store.afterEdit(<b>this</b>);
        }       
    },

    <i>/**
     * Get the value of the named field.
     * @param {String} name The name of the field to get the value of.
     * @<b>return</b> {Object} The value of the field.
     */</i>
    get : <b>function</b>(name){
        <b>return</b> this.data[name]; 
    },

    <i>// private</i>
    beginEdit : <b>function</b>(){
        <b>this</b>.editing = true;
        <b>this</b>.modified = {}; 
    },

    <i>// private</i>
    cancelEdit : <b>function</b>(){
        <b>this</b>.editing = false;
        <b>delete</b> this.modified;
    },

    <i>// private</i>
    endEdit : <b>function</b>(){
        <b>this</b>.editing = false;
        <b>if</b>(this.dirty &amp;&amp; <b>this</b>.store){
            <b>this</b>.store.afterEdit(<b>this</b>);
        }
    },

    <i>/**
     * Usually called by the {@link Ext.data.Store} which owns the Record.
     * Rejects all changes made to the Record since either creation, or the last commit operation.
     * Modified fields are reverted to their original values.
     * &lt;p&gt;
     * Developers should subscribe to the {@link Ext.data.Store#update} event to have their code notified
     * of reject operations.
     */</i>
    reject : <b>function</b>(){
        <b>var</b> m = <b>this</b>.modified;
        <b>for</b>(var n <b>in</b> m){
            <b>if</b>(typeof m[n] != &quot;<b>function</b>&quot;){
                <b>this</b>.data[n] = m[n];
            }
        }
        <b>this</b>.dirty = false;
        <b>delete</b> this.modified;
        <b>this</b>.editing = false;
        <b>if</b>(this.store){
            <b>this</b>.store.afterReject(<b>this</b>);
        }
    },

    <i>/**
     * Usually called by the {@link Ext.data.Store} which owns the Record.
     * Commits all changes made to the Record since either creation, or the last commit operation.
     * &lt;p&gt;
     * Developers should subscribe to the {@link Ext.data.Store#update} event to have their code notified
     * of commit operations.
     */</i>
    commit : <b>function</b>(){
        <b>this</b>.dirty = false;
        <b>delete</b> this.modified;
        <b>this</b>.editing = false;
        <b>if</b>(this.store){
            <b>this</b>.store.afterCommit(<b>this</b>);
        }
    },

    <i>// private</i>
    hasError : <b>function</b>(){
        <b>return</b> this.error != null;
    },

    <i>// private</i>
    clearError : <b>function</b>(){
        <b>this</b>.error = null;
    },

    <i>/**
     * Creates a copy of <b>this</b> record.
     * @param {String} id (optional) A <b>new</b> record id <b>if</b> you don<em>'t want to use <b>this</b> record'</em>s id
     * @<b>return</b> {Record}
     */</i>
    copy : <b>function</b>(newId) {
        <b>return</b> new <b>this</b>.constructor(Ext.apply({}, <b>this</b>.data), newId || <b>this</b>.id);
    }
};</code></pre><hr><div style="font-size:10px;text-align:center;color:gray;">Ext - Copyright &copy; 2006-2007 Ext JS, LLC<br />All rights reserved.</div>
    </body></html>